<template>
 <van-icon
    color="#777"
    :name="value ? 'star' : 'star-o'"
    :class="{collcet: value}"
    @click="onCollect"
    :loading="loading"
  />
</template>

<script>
// 导入接口
import { addCollect, deleteCollec } from '@/api/user'
export default {
  data () {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artID: {
      type: [String, Number, Object],
      required: true
    }
  },
  methods: {
    async onCollect () {
      this.loading = true
      try {
        if (this.value) {
          await deleteCollec(this.artID)
        } else {
          await addCollect(this.artID)
        }
        this.$emit('input', !this.value)
        this.$toast.success(!this.value ? '收藏成功' : '取消收藏')
      } catch (err) {
        this.$toast('操作失败请重试')
      }
      this.loading = false
    }
  }
}
</script>

<style>
.collcet {
  color: springgreen!important;
}
</style>
